home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Classes / RCString / RCStringRegex.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  225 lines

  1. #import <RCString.h>
  2. /*
  3.     Copyright (C) 1992. Bruce Ediger.
  4.  
  5.       This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU Library General Public License.
  7. */
  8.  
  9. #ifdef BSD_REGEXP
  10. #import <regex.h>
  11. #endif
  12.  
  13. #ifdef GNU_REGEXP
  14. // This part is very dependant on directory setup.
  15. // You'll have to modify it.
  16. #import <sys/types.h>
  17. #import "regex-0.11/regex.h"
  18. #endif
  19.  
  20. //
  21. // implementation of methods having to do with regular expressions
  22. //
  23.  
  24. @implementation RCString (Regex)
  25.  
  26. - objectMatching:(char *)aRegex
  27. {
  28.     RCString *orNewString = NULL;
  29.     char *bprString = NULL;
  30.  
  31.     if (aRegex && (bprString = [self subStringMatching:(char *)aRegex]))
  32.     {
  33.         if ((orNewString = [[RCString alloc] init]))
  34.         {
  35.             orNewString->p->s = bprString;
  36.             orNewString->p->l = strlen(bprString) + 1;
  37.             orNewString->p->n = 1;
  38.         }
  39.     }
  40.  
  41.     return orNewString;
  42. }
  43.  
  44. - (char *)subStringMatching:(char *)abvRegex
  45. {
  46.     char *bprString = NULL;
  47. #ifdef BSD_REGEXP
  48.  
  49.     if (abvRegex == NULL)
  50.         return NULL;  // ???
  51.  
  52.     if (p && p->s && p->l > 1) {
  53.         struct regex *spRegex;
  54.         // compile regular expression
  55.         spRegex = (struct regex *)re_compile(abvRegex, yCaseSensitive?0:1);
  56.         if (spRegex == NULL)
  57.             return NULL;
  58.  
  59.         // check match
  60.         if (re_match(p->s,  spRegex) == 1) {
  61.             // Got a match.
  62.             int iMatchLen = (int)spRegex->end - (int)spRegex->start;
  63.             if (iMatchLen > 0) {
  64.                 bprString = malloc(iMatchLen + 1);
  65.                 if (bprString) {
  66.                     bcopy((char *)spRegex->start, bprString, iMatchLen);
  67.                     bprString[iMatchLen] = '\0';
  68.                 }
  69.             }
  70.         } // else, no match, return NULL;
  71.         free(spRegex);  // spRegex guaranteed non-NULL
  72.     }
  73.  
  74. #endif
  75.  
  76. #ifdef GNU_REGEXP
  77.     // uses the Posix regular expression interface to GNU regex-0.11
  78.     regex_t    tsRegex;
  79.     regmatch_t tvMatchBuffer[2];
  80.  
  81.     if (abvRegex == NULL)
  82.         return FALSE;  // ???
  83.  
  84.     // initialize the regex_t struct
  85.     tsRegex.translate = 0;
  86.     tsRegex.fastmap = 0;
  87.     tsRegex.buffer = 0;
  88.     tsRegex.allocated = 0;
  89.  
  90.     if (regcomp(&tsRegex, abvRegex, REG_NEWLINE) != 0) {
  91.         // pattern compilation failed
  92.         // could attempt some corrective action
  93.         return FALSE;
  94.     } else {
  95.         if (p && p->s && p->l > 1
  96.             && (regexec(&tsRegex, p->s, 1, tvMatchBuffer, 0) == 0))
  97.         {
  98.             // found a match, allocate string and copy matched string
  99.             int iMatchLen = tvMatchBuffer[0].rm_eo - tvMatchBuffer[0].rm_so;
  100.             if ((bprString = malloc(iMatchLen + 1))) {
  101.                 bcopy(&(p->s[tvMatchBuffer[0].rm_so]), bprString, iMatchLen);
  102.                 bprString[iMatchLen] = '\0';
  103.             }
  104.         } // else, return is already NULL
  105.     }
  106.  
  107.     regfree(&tsRegex);
  108. #endif
  109.     return bprString;
  110. }
  111.  
  112. - (BOOL)matches:(char *)abvRegex
  113. {
  114.     BOOL yrRetval = FALSE;
  115.  
  116. #ifdef BSD_REGEXP
  117.  
  118.     if (abvRegex == NULL)
  119.         return FALSE;  // ???
  120.  
  121.     if (p && p->s && p->l > 1) {
  122.         struct regex *spRegex;
  123.         // compile regular expression
  124.         spRegex = (struct regex *)re_compile(abvRegex, yCaseSensitive?0:1);
  125.         if (spRegex == NULL)
  126.             return FALSE;
  127.  
  128.         // check match
  129.         if (re_match(p->s,  spRegex) == 1)
  130.             yrRetval = TRUE;
  131.  
  132.         free(spRegex);
  133.     } // else, return is already FALSE
  134.  
  135. #endif
  136.  
  137. #ifdef GNU_REGEXP
  138.     // use the Posix regular expression interface to GNU regex-0.11
  139.     regex_t    tsRegex;
  140.     regmatch_t tvMatchBuffer[2];
  141.  
  142.     if (abvRegex == NULL)
  143.         return FALSE;  // ???
  144.  
  145.     // initialize the regex_t struct
  146.     tsRegex.translate = 0;
  147.     tsRegex.fastmap = 0;
  148.     tsRegex.buffer = 0;
  149.     tsRegex.allocated = 0;
  150.  
  151.     if (regcomp(&tsRegex, abvRegex, REG_NEWLINE) != 0) {
  152.         // pattern compilation failed
  153.         // could attempt some corrective action
  154.         return FALSE;
  155.     } else {
  156.         if (p && p->s && p->l > 1
  157.             && (regexec(&tsRegex, p->s, 1, tvMatchBuffer, 0) == 0))
  158.                     yrRetval = TRUE;
  159.         // else, return is already FALSE
  160.     }
  161.  
  162.     regfree(&tsRegex);
  163. #endif
  164.  
  165.     return yrRetval;
  166. }
  167.  
  168. - replaceSubStringMatching:(char *)abvRegex with:(char *)aString
  169. {
  170. #ifdef BSD_REGEXP
  171.     if (abvRegex != NULL && p && p->s && p->l > 1) {
  172.  
  173.         struct regex *spRegex;
  174.  
  175.         // compile regular expression
  176.         spRegex = (struct regex *)re_compile(abvRegex, yCaseSensitive?0:1);
  177.  
  178.         // check match
  179.         if (aString && spRegex != NULL && re_match(p->s,  spRegex) == 1)
  180.         {
  181.             // Got a match.
  182.             [self replaceStringAt:((int)spRegex->start - (int)p->s)
  183.                 extent:((int)spRegex->end - (int)spRegex->start)
  184.                 with:aString];
  185.         } // else, no match
  186.  
  187.         if (spRegex) free(spRegex); 
  188.     }
  189. #endif
  190.  
  191. #ifdef GNU_REGEXP
  192.     // uses the Posix regular expression interface to GNU regex-0.11
  193.     if (abvRegex) {
  194.         regex_t    tsRegex;
  195.         regmatch_t tvMatchBuffer[2];
  196.  
  197.         if (regcomp(&tsRegex, abvRegex, REG_NEWLINE) != 0) {
  198.             // pattern compilation failed
  199.             // could attempt some corrective action
  200.             [self error:"Compiling a Posix regular expression"];
  201.         } else {
  202.             // initialize the regex_t struct
  203.             tsRegex.translate = 0;
  204.             tsRegex.fastmap = 0;
  205.             tsRegex.buffer = 0;
  206.             tsRegex.allocated = 0;
  207.  
  208.             if (p && p->s && p->l > 1
  209.                 && (regexec(&tsRegex, p->s, 1, tvMatchBuffer, 0) == 0))
  210.             {
  211.                 // found a match
  212.                 [self replaceStringAt:tvMatchBuffer[0].rm_so
  213.                     extent:(tvMatchBuffer[0].rm_eo - tvMatchBuffer[0].rm_so)
  214.                     with:aString];
  215.             } // else, no change to internal string rep
  216.         }
  217.  
  218.         regfree(&tsRegex);
  219.     }
  220. #endif
  221.     return self;
  222. }
  223.  
  224. @end
  225.